﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using ARS.Control.Common.EntityManage;
using ARS.Control.Custom.Entity.Fields;
using ARS.Control.Custom.Entity.Model;

namespace ARS.Control.Custom.Entity.Widget
{
    public class EntityShowPanel:EntityMappingPanel
    {
        /// <summary>
        /// 是否自动创建
        /// </summary>
        public bool IsAutoCreate
        {
            get;
            set;
        }

        public EntityShowPanel()
        {
            base.type = EntityModel.TYPE_SHOW;
        }

        /// <summary>
        /// 一行多少列
        /// </summary>
        public int ColumnNumber = 1;

        protected override void RenderContents(System.Web.UI.HtmlTextWriter output)
        {
            if (IsAutoCreate)
            {
                //output.Write("<table>");
                int index = 0;
                List<Field> list = EntityTableInfo.FieldList.Where(form=>form.IsShow).ToList();
                int count = list.Count();
                string singleHtml = this.GetSingleHtml();
                foreach (var form in list)
                {
                    if (index%ColumnNumber == 0)
                    {
                        output.Write("<tr>");
                    }


                    if (index == count - 1 && index % ColumnNumber != ColumnNumber - 1)
                    {
                        output.Write(string.Format(this.GetLastSingleHtml((ColumnNumber - index % ColumnNumber-1)*2+1), form.Verbose_Name, base.GetFormFieldVale(form)));
                    }
                    else
                    {
                        output.Write(string.Format(singleHtml, form.Verbose_Name, base.GetFormFieldVale(form)));                      
                    }
                    if (index % ColumnNumber == ColumnNumber - 1 || index == count - 1)
                    {
                        output.Write("</tr>");
                    }
                    
                    //output.Write(form.Field.Value);
                    //this.Controls.Add(form);
                    index++;
                }
                //output.Write("</table>");
            }
            else
            {
                base.RenderContents(output);
            }
            
        }
        /// <summary>
        /// 获取单个html，<td>{0}</td><td>{1}</td>
        /// </summary>
        public Func<string> GetSingleHtml = () =>
        {
            return "<td>{0}</td><td>{1}</td>";
        };
        /// <summary>
        /// 最后一行的html
        /// </summary>
        public Func<int,string> GetLastSingleHtml = (index) =>
        {
            return string.Format("<td>{{0}}</td><td colspan='{0}'>{{1}}</td>", index);
        };
    }
}
